package com.mengxuegu.member.controller;


import com.mengxuegu.member.base.Result;
import com.mengxuegu.member.entity.Goods;
import com.mengxuegu.member.req.GoodsREQ;
import com.mengxuegu.member.service.IGoodsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 十一捉一
 * @since 2020-12-01
 */
@RestController
@RequestMapping("/goods")
public class GoodsController {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private IGoodsService goodsService;

    @PostMapping("/list/search/{page}/{size}")
    public Result search(@PathVariable("page") long page,
                         @PathVariable("size") long size,
                         @RequestBody GoodsREQ req) {
        logger.info("查询商品信息列表：page={}, size={}", page, size);
        return goodsService.search(page, size, req);
    }

    @PostMapping
    public Result add(@RequestBody Goods goods) {
        boolean b = goodsService.save(goods);
        if(b) return Result.ok();
        return Result.error("新增商品信息失败");
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Integer id) {
        boolean b = goodsService.removeById(id);
        if(b) return Result.ok();
        return Result.error("删除商品信息失败");
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
        return goodsService.findById(id); // service里先查商品表，再查供应商表
        // return goodsService.findById2(id); // xml形式联表查询，据说上面那个快，所以还是注释掉这个吧。
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Integer id,
                         @RequestBody Goods goods) {
        return goodsService.update(id, goods);
    }

}
